import time
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
import sys
sys.path.append('C:/Project/Python3')
sys.path.append('D:/Project/Python3')

import DataProcess
import Core.WindFunctions as Wind
import Core.Gadget as Gadget
import Core.WindFunctions as Wind

#import Core.DataView as DataView
#database = DataView.DataView.BatchView()
#realtime = DataView.DataView.RealTimeView()

from Core.Config import *
config = Config()
database = config.DataBase()
realtime = config.RealTime()
logger = config.Logger("ScheduleManager", False)

def TestJob():
    t = Gadget.ToUTCDateTime(datetime.datetime.now())
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))


def DailyTask():
    pass

def MorningTask():
    print("Morning Task, Prepare to Open Market @" + str(datetime.now()))

    # ---Start Wind API---
    Wind.w.start()

    # ---Update Instrument List---
    DataProcess.Request_Write_Stock_List(database)

    # ---Update Futures---
    DataProcess.Request_Write_Future_List(database=database, realtime=realtime)

    # ---更新期权列表---
    DataProcess.Request_Write_Option_List(database=database, realtime=realtime)
    pass

def ClosedMarket():
    print("Closed Market, After Market Processing @" + str(datetime.now()))
    pass

def Midnight():
    print("Midnight Processing @" + str(datetime.now()))
    pass

def HourTask():
    print("Hour @" + str(datetime.now()))
    pass

def MinuteTask():
    print("Minute @" + str(datetime.now()))
    logger.info("Minutes")
    pass


# http://www.cnblogs.com/luxiaojun/p/6567132.html

print("Scheduler Manager Started")
schedudler = BlockingScheduler()

now = datetime.now()
start_date = datetime(now.year, now.month, now.day, now.hour, 0, 0)

#
# schedudler.add_job(TestJob, 'interval', seconds=5)
schedudler.add_job(MinuteTask, 'interval', minutes=1, start_date=start_date)
schedudler.add_job(HourTask, 'interval', hours=1, start_date=start_date)

# cron定时调度
schedudler.add_job(MorningTask, 'cron', day_of_week='0-6', hour=8, minute=0, second=0)
schedudler.add_job(ClosedMarket, 'cron', day_of_week='0-6', hour=15, minute=30, second=0)
schedudler.add_job(Midnight, 'cron', day_of_week='0-6', hour=0, minute=0, second=0)
#
schedudler.start()